package itsm.isperp.module.service.app;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.data.mybatis.dao.TreeNodeMapper;
import itsm.isperp.framework.security.domain.RoleResource;
import itsm.isperp.framework.service.IMenuService;
import itsm.isperp.framework.service.TreeNodeService;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.entity.app.AppMenu;
import itsm.isperp.module.repository.app.AppMenuMapper;
import itsm.isperp.module.repository.app.VAppRoleMenuMapper;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

/**
 * 系统菜单服务
 * 
 * @author lizx
 * @date 2013-1-12
 * @version V1.0
 */
@Service
public class AppMenuService extends TreeNodeService<AppMenu> implements
		IMenuService {

	@Resource
	protected AppMenuMapper appMenuMapper;
	@Resource
	protected VAppRoleMenuMapper vAppRoleMenuMapper;

	@Override
	public TreeNodeMapper<AppMenu> getRepository() {
		return this.appMenuMapper;
	}

	/**
	 * 根据唯一标识获取资源菜单
	 * 
	 * @param id
	 *            唯一标识
	 * @return 资源菜单
	 */
	public AppMenu findOne(String id) {
		return this.appMenuMapper.findOne(id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public AppMenu findMenuById(String id) {
		return this.appMenuMapper.findOne(id);
	}

	@Override
	public List<? extends RoleResource> findAllRoleResource() {
		return this.vAppRoleMenuMapper.findAll();
	}

	/**
	 * 根据父级id查找
	 * 
	 * @param parentId
	 *            父级id
	 * @return 系统菜单列表
	 */
	public List<AppMenu> findByParentId(String parentId) {

		JqGridRequest req = new JqGridRequest();
		req.isPageable(false);
		req.setSort("order_num", Direction.ASC);

		if (StringUtils.isEmpty(parentId)) {
			req.addFilterRule("parent_id", JqGridFilterRuleOp.isnull, null);

		} else {
			req.addFilterRule("parent_id", JqGridFilterRuleOp.eq, parentId);
		}
		return this.appMenuMapper.findAllByRequest(req);
	}

	/**
	 * 获取用户具有的顶级菜单目录
	 * 
	 * @param username
	 *            用户名称
	 * @return 列表
	 */
	public List<AppMenu> findUserRootMenu() {
		String username = ContextHolder.getLoginUsername();
		if ("admin".equals(username)) {
			return findByParentId(null);
		}

		JqGridRequest req = new JqGridRequest();

		req.isPageable(false);
		req.addFilterRule("user_name", JqGridFilterRuleOp.eq, username);
		req.setSearchString("parent_id IS NULL OR parent_id=''");
		req.setSort("order_num", Direction.ASC);

		return this.appMenuMapper.findRoleMenuByRequest(req);

	}

	/**
	 * 
	 * @param treenodePath
	 * @param username
	 * @return
	 */
	public List<AppMenu> findUsernameMenuByParentId(String parentId) {

		AppMenu currentMenu = this.getRepository().findOne(parentId);

		String username = ContextHolder.getLoginUsername();

		if ("admin".equals(username)) {
			return this.findByTreenodePath(currentMenu.getTreenodePath());
		}
		JqGridRequest req = new JqGridRequest();

		req.isPageable(false);
		req.addFilterRule("user_name", JqGridFilterRuleOp.eq, username);
		req.addFilterRule("treenode_path", JqGridFilterRuleOp.bw,
				currentMenu.getTreenodePath());
		req.setSort("order_num", Direction.ASC);
		return this.appMenuMapper.findRoleMenuByRequest(req);
	}

	public List<Map<String, Object>> getWebosMenu() {
		JqGridRequest req = new JqGridRequest();

		String username = ContextHolder.getLoginUsername();

		req.isPageable(false);
		req.addFilterRule("user_name", JqGridFilterRuleOp.eq, username);
		req.addFilterRule("use_for_webos", JqGridFilterRuleOp.eq, "1");
		req.addSidx("index_num").addSidx("custom_order_num")
				.addSidx("order_num");
		return this.appMenuMapper.findRoleCustomMenuByRequest(username, req);
	}
	
	public boolean getCurUserRight(String tUrl)
	{
		boolean result=true;
		String username = ContextHolder.getLoginUsername();
		JdbcTemplate jdbcTemplate = ContextHolder
				.getSpringBean("jdbcTemplate");
		String sql1="SELECT * FROM app_menu WHERE url='"+tUrl+"'";
		List<Map<String,Object>> list1=jdbcTemplate.queryForList(sql1);
		if(list1.size()>0)
		{
			String sql2="SELECT * FROM app_menu a LEFT JOIN app_role_menu b ON a.id=b.app_menu_id LEFT JOIN app_user_role c ON b.app_role_id=c.app_role_id WHERE c.app_user_name='"+username+"' AND a.url='"+tUrl+"'";
			List<Map<String,Object>> list2=jdbcTemplate.queryForList(sql2);
			if(list2.size()==0)
			{
				result=false;
			}
		}
		return result;
	}

}
